package com.market.domain.transients;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

import javax.faces.context.FacesContext;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class ReportHeaderFooter extends PdfPageEventHelper implements
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8289985913410835524L;

	private String leftHeader;
	private String centerHeader;
	private String rightHeader;
	private String leftFooter;
	private String centerFooter;
	private String rightFooter;

	public String getLeftHeader() {
		return leftHeader;
	}

	public void setLeftHeader(String leftHeader) {
		this.leftHeader = leftHeader;
	}

	public String getCenterHeader() {
		return centerHeader;
	}

	public void setCenterHeader(String centerHeader) {
		this.centerHeader = centerHeader;
	}

	public String getRightHeader() {
		return rightHeader;
	}

	public void setRightHeader(String rightHeader) {
		this.rightHeader = rightHeader;
	}

	public String getLeftFooter() {
		return leftFooter;
	}

	public void setLeftFooter(String leftFooter) {
		this.leftFooter = leftFooter;
	}

	public String getCenterFooter() {
		return centerFooter;
	}

	public void setCenterFooter(String centerFooter) {
		this.centerFooter = centerFooter;
	}

	public String getRightFooter() {
		return rightFooter;
	}

	public void setRightFooter(String rightFooter) {
		this.rightFooter = rightFooter;
	}

	@Override
	public void onEndPage(PdfWriter writer, Document document) {

		String realPath = FacesContext.getCurrentInstance()
				.getExternalContext().getRealPath("");
		String logo = realPath + File.separator + "images" + File.separator
				+ "logo.png";

		Rectangle rect = writer.getBoxSize("art");
		Font titleFont = new Font(Font.FontFamily.COURIER, 12f);
		
		titleFont.setColor(BaseColor.RED);
		Font subTitleFont = new Font(Font.FontFamily.COURIER, 8f);
		Font footerFont = new Font(Font.FontFamily.TIMES_ROMAN, 5f);

		if (this.leftHeader != null) {
			ColumnText.showTextAligned(writer.getDirectContent(),
					Element.ALIGN_LEFT, new Phrase(this.leftHeader,
							subTitleFont), rect.getLeft(), rect.getTop() - 47f,
					0);
		}

		if (this.centerHeader != null) {
			ColumnText.showTextAligned(writer.getDirectContent(),
					Element.ALIGN_CENTER, new Phrase(centerHeader, titleFont),
					(rect.getLeft() + rect.getRight()) / 2,
					rect.getTop() - 30f, 0);
		}
		if (this.rightHeader != null) {
			ColumnText.showTextAligned(writer.getDirectContent(),
					Element.ALIGN_RIGHT, new Phrase(this.rightHeader,
							subTitleFont), rect.getRight(), rect.getTop() - 47f,
					0);
		}
		if(this.leftFooter==null || this.leftFooter.isEmpty()==true){
			this.leftFooter="Tel:(514)699-4508";
		}

		if (this.leftFooter != null) {
			ColumnText.showTextAligned(writer.getDirectContent(),
					Element.ALIGN_LEFT,
					new Phrase(this.leftFooter, footerFont), rect.getLeft(),
					rect.getBottom() - 5f, 0);
		}

		if (this.centerFooter == null || this.centerFooter.isEmpty()) {
			this.centerFooter = "www.QuebecFresh.com";
		}

		ColumnText.showTextAligned(writer.getDirectContent(),
				Element.ALIGN_CENTER,
				new Phrase(this.centerFooter, footerFont),
				(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 5f,
				0);

		if (this.rightFooter == null || this.rightFooter.isEmpty()) {
			this.rightFooter = "Printed on " + new Date().toString();
		}

		ColumnText.showTextAligned(writer.getDirectContent(),
				Element.ALIGN_RIGHT, new Phrase(this.rightFooter, footerFont),
				rect.getRight(), rect.getBottom() - 5f, 0);

	}

	@Override
	public void onStartPage(PdfWriter writer, Document document) {
//		String realPath = FacesContext.getCurrentInstance()
//				.getExternalContext().getRealPath("");
//		String logo = realPath + File.separator + "images" + File.separator
//				+ "logo.png";
//
//		Rectangle rect = writer.getBoxSize("art");
//		Font titleFont = new Font(Font.FontFamily.COURIER, 14f);
//		
//		titleFont.setColor(BaseColor.RED);
//		Font subTitleFont = new Font(Font.FontFamily.COURIER, 10f);
//		Font footerFont = new Font(Font.FontFamily.TIMES_ROMAN, 8f);
//
//		if (this.leftHeader != null) {
//			ColumnText.showTextAligned(writer.getDirectContent(),
//					Element.ALIGN_LEFT, new Phrase(this.leftHeader,
//							subTitleFont), rect.getLeft(), rect.getTop() + 7f,
//					0);
//		}
//
//		if (this.centerHeader != null) {
//			ColumnText.showTextAligned(writer.getDirectContent(),
//					Element.ALIGN_CENTER, new Phrase(centerHeader, titleFont),
//					(rect.getLeft() + rect.getRight()) / 2,
//					rect.getTop() + 10f, 0);
//		}
//		if (this.rightHeader != null) {
//			ColumnText.showTextAligned(writer.getDirectContent(),
//					Element.ALIGN_RIGHT, new Phrase(this.rightHeader,
//							subTitleFont), rect.getRight(), rect.getTop() + 7f,
//					0);
//		}
//		if(this.leftFooter==null || this.leftFooter.isEmpty()==true){
//			this.leftFooter="Tel:(514)699-4508";
//		}
//
//		if (this.leftFooter != null) {
//			ColumnText.showTextAligned(writer.getDirectContent(),
//					Element.ALIGN_LEFT,
//					new Phrase(this.leftFooter, footerFont), rect.getLeft(),
//					rect.getBottom() - 5f, 0);
//		}
//
//		if (this.centerFooter == null || this.centerFooter.isEmpty()) {
//			this.centerFooter = "www.QuebecFresh.com";
//		}
//
//		ColumnText.showTextAligned(writer.getDirectContent(),
//				Element.ALIGN_CENTER,
//				new Phrase(this.centerFooter, footerFont),
//				(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 5f,
//				0);
//
//		if (this.rightFooter == null || this.rightFooter.isEmpty()) {
//			this.rightFooter = "Printed on " + new Date().toString();
//		}
//
//		ColumnText.showTextAligned(writer.getDirectContent(),
//				Element.ALIGN_RIGHT, new Phrase(this.rightFooter, footerFont),
//				rect.getRight(), rect.getBottom() - 5f, 0);
	}
	
	

}
